package com.sto.stoapp.activity.sales;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.SoleAdapter;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.entity.FunctionEntity;
import com.sto.stoapp.listener.OnBackClickListener;
import com.sto.stoapp.listener.StoOnItemClickListener;
import com.sto.stoapp.thread.SmzApkDownloadThread;
import com.sto.stoapp.widget.BaseDialog;
import com.sto.stoapp.widget.StoHomeActivity;

import java.util.ArrayList;
import java.util.List;

public class SalesReceivedPieceActivity extends StoHomeActivity {
    // 按键映射
    private static final int[] MAPP_TO_KEY = { R.string.simple_recv_title, R.string.advanced_recv_title};
	private ListView RecvFuncList;
	private SoleAdapter RecvListAdapter;
	private String busiType="";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sto_sales_received_piece);
		
		Bundle extras = getIntent().getExtras();
		busiType = extras.getString("busiType");
		String[] data = busiType.split("\\|");
		if(data.length > 1 && data[1].equals("1")){
			this.setTitleBottom(R.string.h_received_piece,
					HomeBroadAction.SALES_MAN_BROAD);
		}else{
			this.setTitleBottom(R.string.received_piece,
					HomeBroadAction.SALES_MAN_BROAD);
		}
		initWidget();
	}
	
	private void initWidget() {
		RecvFuncList = (ListView)findViewById(R.id.receivedFunc_list);
		RecvListAdapter = new SoleAdapter(this, getResourceList());
		RecvFuncList.setAdapter(RecvListAdapter);
		
		RecvFuncList.setOnItemClickListener(new StoOnItemClickListener(this) {
			
			@Override
			public void KcOnItemClickListener(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				arg1.setSelected(true);
				TextView title = (TextView) arg1.findViewById(R.id.res_test);
				clickItemEvent(title.getText().toString());
			}
			
		});
			
	}
	
	private void clickItemEvent(String title) {
		// 简单收件
		if (title.equals(getText(R.string.simple_recv_title))) {
			if(!getSecurityPackageInfo()){
				// 判断是不是浙江省
				if(checkSiteZJ()){
					// 提示下载实名制插件
					downNotice(this);
					return;
				}
			}
			gotoSimpleRecvActivity("个人快件");
		}
		// 高级收件
		else if (title.equals(getText(R.string.advanced_recv_title))) {
			/*Intent intent = new Intent(this, SalesadvancedRecvActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);*/
			gotoSimpleRecvActivity("普通收件");
		}else {

		}
	}

	private void downSmzFile() {
		new SmzApkDownloadThread(this).start();

	}
	private void downNotice(final Context context) {
		final BaseDialog dialog = new BaseDialog(context);
		dialog.setTitles("必须下载实名制插件才能操作，文件大小10多兆，建议在WIFI环境下下载！");
		dialog.show();
		dialog.getvMessage().setVisibility(View.GONE);
		dialog.getvPresentation().setVisibility(View.GONE);
		dialog.setOnBackClickListener(new OnBackClickListener() {

			@Override
			public void onBackClickListener(boolean res) {
				dialog.dismiss();
				if (res) {
					downSmzFile();
				}

			}
		});
	}


	private void gotoSimpleRecvActivity(String title) {
		Intent intent = new Intent(this, SalesSimpleRecvActivity.class);
		intent.putExtra("busiType", busiType);
		intent.putExtra("titleType", title);
		startActivity(intent);
	}
	
	private List<FunctionEntity> getResourceList() {
		List<String> resList = new ArrayList<String>();
		resList.add(getText(R.string.simple_recv_title).toString());
		resList.add(getText(R.string.advanced_recv_title).toString());
		//resList.add(getText(R.string.advanced_recv_title).toString());
		
		List<FunctionEntity> functionList = new ArrayList<FunctionEntity>();
		for (String res : resList) {
			FunctionEntity entity = new FunctionEntity();
			entity.setTitle(res);
			functionList.add(entity);
		}
		return functionList;
	}
	


	

}
